#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "tingfeng"
# Date: 2020/4/29


import os
import sys
sys.path.append('D:/pycharm/appium_unittest')
import HTMLTestRunner
import unittest
#from BeautifulReport import BeautifulReport
import threading
import multiprocessing #进程
from util.server import Server
from appium import webdriver
from business.login_business import LoginBusiness
from util.write_user_command import WriteUserCommand
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

class ParameTestCase(unittest.TestCase):
    def __init__(self,methodName='runTest',parame=None):
            super(ParameTestCase,self).__init__(methodName)
            global parames
            parames = parame

class CaseTest(ParameTestCase):
    @classmethod
    def setUpClass(cls):
        print("setUpClasss---->"),parames
        # cls.login_business = LoginBusiness(parames)

    def setUp(self):
        print("this is setup\n")

    def test_01(self):
        print("test case 里的参数"),parames
        login_business = LoginBusiness(parames)
        login_business.login_method()



    @unittest.skip("CaseTest")
    def test_02(self):
        print("this is case2\n")
        self.login_business.hairdressing_waxing_hexiao()

    def tearDown(self):
        time.sleep(1)
        print('this is teardown\n')

    @classmethod
    def tearDownClass(cls):
        time.sleep(1)
        print("this is class teardown\n")

def appium_init():
    server = Server()
    server.main()

def get_suite(i):

    print("get_suite",i)
    suite = unittest.TestSuite()
    suite.addTest(CaseTest("test_01",parame=i))
    suite.addTest(CaseTest("test_02",parame=i))

    timestr = time.strftime('%Y-%m-%d%H%M%S', time.localtime(time.time()))
    html_file="D:/pycharm/appium_unittest/Report/"+timestr+"油呗测试报告("+str(i)+").html"
    fp = open(html_file,"wb")
    HTMLTestRunner.HTMLTestRunner(stream=fp).run(suite)




    # current_path = '/Users/tingfeng/PycharmProjects/appium_unittest'
    # report_path = os.path.join(current_path, "Report")
    # report_title = '油呗C端测试报告' + timestr
    # desc = '首页模块'
    # report_file = str(i)+'油呗C端测试报告' + timestr + '.html'
    # #testsuite.addTest(unittest.makeSuite(homepage))
    # suite = unittest.TestSuite()
    # suite.addTest(CaseTest("test_01", parame=i))
    # suite.addTest(CaseTest("test_02", parame=i))
    # # testsuite.addTest(unittest.makeSuite(middle))
    # #with open(report_file, 'wb') as report:
    # run = BeautifulReport(suite)
    # run.report(description=desc, filename=report_title, log_path=report_path)
    #
    # # 测试报告存储路径
    # report_dir = "./report/"
    # # 将测试报告文件夹下的所有文件名作为一个列表返回
    # lists = os.listdir(report_dir)
    # # 对所有测试报告按照生成时间进行排序
    # lists.sort(key=lambda filename: os.path.getmtime(report_dir + filename))
    # # 获取最新的测试报告
    # recent = lists[-1]

def get_count():
        write_user_file = WriteUserCommand()
        count = write_user_file.get_file_lines()
        return count

if __name__ == '__main__':
    appium_init()
    threads = []
    for i in range(get_count()):
            print(i)
            t = multiprocessing.Process(target=get_suite,args=(i,))
            threads.append(t)
    for j in threads:
        j.start()
        j.join()
